package com.huipinche.view.impl;

import com.huipinche.dao.bean.Journey;
import com.huipinche.view.HallPageView;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import com.huipinche.view.utils.*;
import com.huipinche.service.impl.*;

public class HallPageViewimpl  implements HallPageView {
    final int WIDTH = 510;
    final int HEIGHT = 451;
    private int page = 0;
    private boolean flag = true;
    private BackGroundPanel contentPane;
    private JFrame jFrame;
    private JTextField textField;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    /*public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HallPageViewimpl frame = new HallPageViewimpl();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }*/

    /**
     * Create the frame.
     */
    public void initialize(){
        jFrame = new JFrame("惠拼车-拼单大厅");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        try {
            jFrame.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo3.jpeg"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Image image=new ImageIcon(PathUtils.getRealPath("bigbackground4.png")).getImage();
        contentPane = new BackGroundPanel(image);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);

        JButton btnNewButton = new JButton("返回主页");
        btnNewButton.setBounds(0, 0, 89, 23);
        HallPageViewimpl temp = this;
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                temp.page = 200;//homepage
                temp.flag = false;
            }
        });
        contentPane.setLayout(null);
        contentPane.add(btnNewButton);

        textField = new JTextField();
        textField.setText("              拼单大厅");
        textField.setBounds(90, 31, 290, 23);
        textField.setOpaque(false);
        textField.setFont(new Font("楷体",Font.BOLD,20));
        textField.setForeground(Color.black);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_2 = new JTextField("拼车信息：......");
        textField_2.setBounds(0, 185, 475, 97);
        contentPane.add(textField_2);
        textField_2.setOpaque(false);
        textField_2.setFont(new Font("楷体",Font.BOLD,20));
        textField_2.setForeground(Color.white);
        textField_2.setColumns(10);

        textField_3 = new JTextField("拼车信息：......");
        textField_3.setBounds(0, 292, 475, 87);
        textField_3.setOpaque(false);
        textField_3.setFont(new Font("楷体",Font.BOLD,20));
        textField_3.setForeground(Color.white);
        contentPane.add(textField_3);
        textField_3.setColumns(10);

        JPanel panel = new JPanel();
        panel.setBounds(0, 94, 475, 81);
        panel.setOpaque(false);
        contentPane.add(panel);
        panel.setLayout(null);

        JButton btnNewButton_1 = new JButton("申请加入");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new GeneralPageimpl("加入成功，请等待信息");
                temp.page = 211;  //申请加入成功，请等待回复
                temp.flag = false;
            }
        });
        btnNewButton_1.setBounds(358, 10, 107, 23);
        panel.add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("私聊发布者");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new GeneralPageimpl("此功能暂未开发");

            }
        });
        btnNewButton_2.setBounds(358, 44, 107, 23);
        panel.add(btnNewButton_2);

        JTextArea textArea = new JTextArea("拼车信息:......");
        textArea.setBounds(0, 0, 475, 81);
        textArea.setOpaque(false);
        textArea.setFont(new Font("楷体",Font.BOLD,20));
        textArea.setForeground(Color.white);
        panel.add(textArea);

        textField_1 = new JTextField();
        textField_1.setBounds(90, 64, 290, 21);
        textField_1.setOpaque(false);
        textField_1.setFont(new Font("楷体",Font.BOLD,20));
        textField_1.setForeground(Color.white);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JButton lblNewLabel = new JButton("搜索");
        lblNewLabel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                temp.page = 231;//findpage
                temp.flag = false;
            }
        });
        lblNewLabel.setFont(new Font("楷体",Font.BOLD,20));
        lblNewLabel.setForeground(Color.black);
        lblNewLabel.setBounds(0, 64, 89, 23);
        contentPane.add(lblNewLabel);
        jFrame.setVisible(true);
        jFrame.setResizable(false);
    }

    @Override
    public int HallPage(boolean flag) throws InterruptedException {
            this.flag = flag;
            initialize();
            Journey[] journeys= new mainJourneyServiceimpl().mainJourney();
        //    System.out.println(journeys);
           while(this.flag){
            Thread.sleep(200);
         };
            switch (page){
                case 200:
                    jFrame.removeNotify();
                    return 200;//主页
                case 211:
                    jFrame.removeNotify();
                    return 211;//等待页面
                case 231:
                    jFrame.removeNotify();
                    return 231;//搜索页面
            }
        return 0;
    }
}
